package com.weedsdream.app

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import com.weedsdream.BaseActivity
import com.weedsdream.app.compose.DrawBoardMainUi
import com.weedsdream.app.floatWindow.RequestPermissionLauncher
import com.weedsdream.app.struct.FileOpe
import com.weedsdream.function.inCoroutine
import kotlinx.coroutines.delay

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { DrawBoardMainUi(false) }
        //注册文件操作生命周期监听
        FileOpe.initActivityResultRegistry(this)
        //加载本地数据
        FileOpe.readColorSet()
        FileOpe.readAutoProject()
        FileOpe.readProjectMenu()
        //每分钟自动保存
        inCoroutine {
            while (true) {
                delay(60000)
                FileOpe.saveColorSet()
                FileOpe.saveAutoProject()
            }
        }
        //申请权限
        RequestPermissionLauncher.apply {
            floatingWindow =
                registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                    floatingWindowFunc()
                }
        }
    }

    override fun onStop() {
        super.onStop()
        //自动保存数据
        FileOpe.saveColorSet()
        FileOpe.saveAutoProject()
    }
}